// 获取tab栏年月日的日期

function getIconDate(field){
    const date = new Date()
    switch (field) {
        case 'day':
            return date.getDate().toString().padStart(2,'0')  //padstart补0
        case 'month':
            return (date.getMonth()+1).toString().padStart(2,'0')
        case 'year':
            return date.getFullYear().toString().substring(2)

    }
}

//获取当前日期
function getNowDate(field){
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth()+1
    const day = date.getDate()

    switch(field){
        case 'day':
            return year+'-'+month+'-'+day
        case 'month':
            return year+'-'+month
        case 'year':
            return year

    }
}

//格式化中文日期
function formatChsDate(date,field){
    const arr = date.split('-')
    switch(field){
        case 'day':
            return arr[0]+'年'+arr[1]+'月'+arr[2]+'日'
        case 'month':
            return arr[0]+'年'+arr[1]+'月'
        case 'year':
            return arr[0]+'年'
    }
}

// 格式化用户输入的日期
function formatUserDate(date){
    let year = ''
    let month = ''
    let day = ''
    switch(date.length){
        case 8:
            //20220907 ->2020-9-7
            year = date.substring(0,4)
            month = Number(date.substring(4,6))
            day = Number(date.substring(6,8))
            return year+'-'+month+'-'+day
        case 6:
            //20220907 ->2020-9-7
            year = date.substring(0,4)
            month = Number(date.substring(4,6))
            return year+'-'+month
        case 4:
            return date
    }
}

export {
    getIconDate,
    getNowDate,
    formatChsDate,
    formatUserDate
}
